﻿using System;
using System.Globalization;
using System.Data;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using MySql.Data.MySqlClient;
using MySql.Data.Types;
using TAMS;

namespace TAMS.Faculty_Member
{
    public partial class Fac_ViewSchedule : System.Web.UI.Page
    {
        MySqlConnection conn;
        MySqlCommand sqlCommand;
        MySqlDataReader readVals;
        string strProvider = Resource1.databaseConnection;
        string id;

        protected void Page_Load(object sender, EventArgs e)
        {
            // Connect to the Database
            conn = new MySqlConnection(strProvider);
            conn.Open();

            // Get the id from querystring
            id = Request.QueryString["id"];

            if (id == null)
                id = "20000001";

            string strSQL = "SELECT * FROM GradStudent WHERE sid = " + id;

            sqlCommand = new MySqlCommand(strSQL, conn);
            readVals = sqlCommand.ExecuteReader();

            while (readVals.Read())
                HeadLabel.Text = "Name: " + readVals.GetString("s_name");
            readVals.Close();

            // Starting to Make Schedule
            DateTime firstDayInweek = GetFirstDayOfWeek(DateTime.Now);
            DataTable dt;
            dt = new DataTable();
            dt.Columns.Add("start", typeof(DateTime));
            dt.Columns.Add("end", typeof(DateTime));
            dt.Columns.Add("name", typeof(string));
            dt.Columns.Add("id", typeof(string));

            strSQL = "SELECT S.schId, S.sch_startTime, S.sch_endTime FROM GradStudent GS, Schedule S, HasSchedule HS WHERE S.schId = HS.schId AND GS.sid = HS.sid AND GS.sid = " + id;

            sqlCommand = new MySqlCommand(strSQL, conn);
            readVals = sqlCommand.ExecuteReader();

            DataRow dr;
            while (readVals.Read())
            {
                dr = dt.NewRow();
                dr["id"] = readVals.GetString("schId");

                dr["start"] = Convert.ToDateTime(readVals.GetString("sch_startTime"));
                dr["end"] = Convert.ToDateTime(readVals.GetString("sch_endTime"));

                dr["name"] = "Free";
                dt.Rows.Add(dr);
            }

            if (!IsPostBack)
            {
                calendarFree.StartDate = DayPilot.Utils.Week.FirstDayOfWeek(firstDayInweek);
                calendarFree.DataSource = dt;
                calendarFree.DataEndField = "end";
                calendarFree.DataStartField = "start";
                calendarFree.DataValueField = "id";
                calendarFree.DataTextField = "name";
                calendarFree.DataBind();
                readVals.Close();
            }
            conn.Close();
        }

        protected void ReturnPage(object sender, EventArgs e)
        {
            Response.Redirect("Fac_ListTA.aspx");
        }

        protected DateTime GetFirstDayOfWeek(DateTime dayInWeek)
        {
            CultureInfo defaultCultureInfo = CultureInfo.CurrentCulture;
            DayOfWeek firstDay = defaultCultureInfo.DateTimeFormat.FirstDayOfWeek;
            DateTime firstDayInWeek = dayInWeek.Date;
            while (firstDayInWeek.DayOfWeek != firstDay)
                firstDayInWeek = firstDayInWeek.AddDays(-1);

            return firstDayInWeek;
        }
    }
}